package com.bw.counter.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.base.constants.RoutePath
import com.bw.common.view.BaseMvvmActivity
import com.bw.counter.R
import com.bw.counter.adapter.ChargeAdapter
import com.bw.counter.databinding.ActivityCountDetailBinding
import com.bw.counter.entity.CounterData
import com.bw.counter.net.CounterViewModel
/**
 *  智慧园区-05-收银台 -v1.1
 *  2025/05/09
 */
@Route(path = RoutePath.PATH_Count_DETAIL)
class CountDetailActivity : BaseMvvmActivity<ActivityCountDetailBinding,CounterViewModel>() {
    @Autowired // 使用 @Autowired 注解自动注入传递过来的参数
    @JvmField
    var detail : CounterData? = null

    lateinit var chargeAdapter: ChargeAdapter
    override fun initView() {
        //注入 参数
        ARouter.getInstance().inject(this)
        Log.i("----ss", "initView: $detail")
        chargeAdapter = ChargeAdapter()
        binding.re.adapter = chargeAdapter
        binding.re.layoutManager = LinearLayoutManager(this)
        binding.detailBar.ivBack.setOnClickListener { finish() }
        binding.detailBar.tvTitle.text = "账单详情"
        binding.con1.setOnClickListener {
            ARouter.getInstance().build(RoutePath.PATH_Count_Code_Detail)
                .withString("entity",detail?.payerName)
                .withString("item",detail?.paymentItemIds)
                .navigation()
        }
        detail.let {
            if (it != null) {
                val replace = it.paymentItemIds.replace(",", "")
                binding.tv1.text = "天骄世家七区/03栋/2单元/${replace}"
                binding.name.text = "户主：${it.payerName}"
                binding.phone.text = "联系电话: ${it.contactNumber}"
                binding.ALLPrice.text = "账单金额:￥ ${it.totalAmount}"
            }
        }
        // 物业费 有列表请求
        vm.getCharge(2)
        vm.chargeSuccess.observe(this){
            chargeAdapter.setList(it)
            //默认全选
            binding.AllSelect.isChecked = true
            chargeAdapter.setAllSelected(true)
        }
        chargeAdapter.setOnItemClickListener { adapter, view, position ->
            val chargeData = chargeAdapter.data[position]
            ARouter.getInstance().build(RoutePath.PATH_Count_WuYe_Detail)
                .withSerializable("charge",chargeData)
                .navigation()
        }
    }

    override fun initData() {
        binding.bu.setOnClickListener {
            val totalPrice = chargeAdapter.getSelectTotal()
            ARouter.getInstance().build(RoutePath.PATH_Count_Code_Detail)
                .withString("totalPrice", "$totalPrice")
                .navigation()
        }
        //设置适配器选中状态 监听
        chargeAdapter.onItemCheckedChangeListener = { position,isChecked->
            upDataTotalPrice()
        }
        chargeAdapter.onAllCheckedChangeListener={ isAllChecked->
            binding.AllSelect.isChecked = isAllChecked
            upDataTotalPrice()
        }

        //全选按钮事件
        binding.AllSelect.setOnCheckedChangeListener { compoundButton, b ->
            chargeAdapter.setAllSelected(b)
        }
    }

    private fun upDataTotalPrice() {
        val total = chargeAdapter.getSelectTotal()
        binding.money.text = "￥${total}.00"
//        binding.ALLPrice.text = "￥${total}.00"
    }


}